//
//  ViewController.m
//  打开相机和相册Demo
//
//  Created by SKY on 15/11/26.
//  Copyright © 2015年 翊sky. All rights reserved.
//

#import "ViewController.h"

@interface ViewController ()<UIImagePickerControllerDelegate>

@property (nonatomic,weak) UIImagePickerController *imagePC1;
@property (nonatomic,weak) UIImagePickerController *imagePC2;

@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view, typically from a nib.
    
    //模拟器无法测试
    
    UIButton *bt1 = [UIButton buttonWithType:UIButtonTypeCustom];
    [bt1 setFrame:CGRectMake(100, 100, 100, 50)];
    [bt1 setTitle:@"打开相机" forState:UIControlStateNormal];
    [bt1 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [bt1 addTarget:self action:@selector(clickCamera:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bt1];
    
    UIButton *bt2 = [UIButton buttonWithType:UIButtonTypeCustom];
    [bt2 setFrame:CGRectMake(250, 100, 100, 50)];
    [bt2 setTitle:@"打开相册" forState:UIControlStateNormal];
    [bt2 setTitleColor:[UIColor redColor] forState:UIControlStateNormal];
    [bt2 addTarget:self action:@selector(clickPhoto:) forControlEvents:UIControlEventTouchUpInside];
    [self.view addSubview:bt2];

    
}
-(void)clickCamera:(UIButton *)btn{
    UIImagePickerControllerSourceType sourceType;
    if ([UIImagePickerController isSourceTypeAvailable: UIImagePickerControllerSourceTypeCamera]) {
        sourceType = UIImagePickerControllerSourceTypeCamera;
    }
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    self.imagePC1 = imagePickerController;
    imagePickerController.delegate = self;
    imagePickerController.allowsEditing = YES;
    imagePickerController.sourceType = sourceType;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
-(void)clickPhoto:(UIButton *)btn{
    
    UIImagePickerController *imagePickerController = [[UIImagePickerController alloc]init];
    self.imagePC2 = imagePickerController;
    imagePickerController.delegate = self;
    if ([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypePhotoLibrary]) {
        UIImagePickerControllerSourceType sourceType = UIImagePickerControllerSourceTypePhotoLibrary;
        imagePickerController.sourceType = sourceType;
        imagePickerController.mediaTypes = [UIImagePickerController availableMediaTypesForSourceType:imagePickerController.sourceType];
    }
    imagePickerController.allowsEditing = NO;
    [self presentViewController:imagePickerController animated:YES completion:nil];
}
#pragma mark -UIImagePickerController代理方法-
-(void)imagePickerController:(UIImagePickerController *)picker didFinishPickingMediaWithInfo:(NSDictionary<NSString *,id> *)info{
    NSLog(@"点击use之后触发");
    if (picker.sourceType == UIImagePickerControllerSourceTypePhotoLibrary) {
        
        UIImage *image = [info objectForKey:UIImagePickerControllerOriginalImage];
        /* 此处info 有六个值
         * UIImagePickerControllerMediaType; // an NSString UTTypeImage)
         * UIImagePickerControllerOriginalImage;  // a UIImage 原始图片
         * UIImagePickerControllerEditedImage;    // a UIImage 裁剪后图片
         * UIImagePickerControllerCropRect;       // an NSValue (CGRect)
         * UIImagePickerControllerMediaURL;       // an NSURL
         * UIImagePickerControllerReferenceURL    // an NSURL that references an asset in the AssetsLibrary framework
         * UIImagePickerControllerMediaMetadata    // an NSDictionary containing metadata from a captured photo
         */
        // 保存图片至本地，方法见下文
        [self saveImage:image withName:@"currentImage.png"];
        //从本地取出图片
        /*
        NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:@"currentImage.png"];
        
        UIImage *savedImage = [[UIImage alloc] initWithContentsOfFile:fullPath];
         */
    }
    if (picker.sourceType == UIImagePickerControllerSourceTypeCamera) {
        //方法和相册一致
    }
    
}
-(void)imagePickerControllerDidCancel:(UIImagePickerController *)picker{
    NSLog(@"点击取消后触发");
    [self dismissViewControllerAnimated:YES completion:nil];
    
}
#pragma mark - 保存图片至沙盒
- (void) saveImage:(UIImage *)currentImage withName:(NSString *)imageName
{
    
    NSData *imageData = UIImageJPEGRepresentation(currentImage, 0.5);
    // 获取沙盒目录
    
    NSString *fullPath = [[NSHomeDirectory() stringByAppendingPathComponent:@"Documents"] stringByAppendingPathComponent:imageName];
    // 将图片写入文件
    
    [imageData writeToFile:fullPath atomically:NO];
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
